package com.huitone.gddw.controller.analysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmCallTest;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.ISmCallTestService;
import com.huitone.gddw.service.dto.analysis.SmCallTestDTO;
import com.huitone.gddw.utils.SysLoginUtil;

@Controller
@RequestMapping(value="smCallTest")
public class SmCallTestController extends CommonController {

	private static final Logger LOGGER = Logger.getLogger(SmCallTestController.class);
	
	@Autowired
	private ISmCallTestService smCallTestService;
	
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<SmCallTest> query(QueryPage<SmCallTest> page, HttpServletRequest request){
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询拨测短信信息";
		Map<String, Object> params = new HashMap<String, Object>();
		String testPhone = request.getParameter("testPhone");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		params.put("testPhone", testPhone);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		page.setParams(params);
		QueryPage<SmCallTest> result = null;
		try {
			result = smCallTestService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询拨测短信信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(SmCallTest entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加拨测短信信息";
		boolean isSuccess = true;
		String msg;
		try {
			QueryPage<SmCallTest> selectPage = smCallTestService.selectPage(new QueryPage<SmCallTest>());
			if(selectPage.getResult()!=null&&selectPage.getResult().size()> 0){
				update(entity);
				msg = "保存成功";		
			}else{
				smCallTestService.save(entity);
				msg = "保存成功";
			}	
			
		} catch (Exception e) {
			LOGGER.error("保存拨测短信信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(SmCallTest entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改拨测短信信息";
		boolean isSuccess = true;
		String msg;
		try {
			SmCallTest smCallTest = smCallTestService.selectByPrimaryKey(entity.getRecId());
			smCallTest.setTestPhone(entity.getTestPhone());
			smCallTest.setTestContent(entity.getTestContent());
			smCallTest.setGateLinkId(entity.getGateLinkId());
			smCallTest.setSendTime(entity.getSendTime());
			smCallTest.setRemark(entity.getRemark());
			smCallTestService.update(smCallTest);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改拨测短信信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	private Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除拨测短信信息";
		boolean isSuccess = true;
		String msg;
		try {
			smCallTestService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除拨测短信信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public SmCallTest toUpdate(String recId) {
		try {
			SmCallTest entity = smCallTestService.selectByPrimaryKey(recId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new SmCallTest();
		}
	}
	
	@ResponseBody
	@RequestMapping(value="getCallTest")
	public List<SmCallTestDTO> getCallTest(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		List<SmCallTestDTO> list = smCallTestService.getRecord(params);
		return list;
	}
	
}
